---
title: "Thymeleaf Snippets"
categories:
- java
- thymeleaf
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgb14b446">Snippets</a>
<ul>
<li><a href="#org9c6ec75">在javascript中使用action传递的model</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orgb14b446">
<h2 id="orgb14b446">Snippets</h2>
<div class="outline-text-2" id="text-orgb14b446">
</div>
<div class="outline-3" id="outline-container-org9c6ec75">
<h3 id="org9c6ec75">在javascript中使用action传递的model</h3>
<div class="outline-text-3" id="text-org9c6ec75">
<blockquote>
<p>
<a href="https://stackoverflow.com/questions/25687816/setting-up-a-javascript-variable-from-spring-model-by-using-thymeleaf">https://stackoverflow.com/questions/25687816/setting-up-a-javascript-variable-from-spring-model-by-using-thymeleaf</a>
<a href="http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#script-inlining-javascript-and-dart">http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#script-inlining-javascript-and-dart</a>
</p>
</blockquote>
<p>
行内表达式： {% raw %} [ [ ...  ]] {% endraw %} ，将表达式用两个中括号包围，不仅javascript，html中也能如此使用。For example:
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">AccountController.java</span>
<span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">String</span> <span style="font-weight: bold;">login</span>(<span style="font-weight: bold; text-decoration: underline;">ModelMap</span> <span style="font-weight: bold; font-style: italic;">map</span>){
    map.addAttribute(<span style="font-style: italic;">"timestamp"</span>, <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">Date</span>().getTime());
    <span style="font-weight: bold;">return</span> <span style="font-style: italic;">"account/login"</span>;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-html">// login.html
&lt;<span style="font-weight: bold;">script</span>&gt;
...
var timestamp = '[[${timestamp}]]';
// result: var timestamp = '1521260482702';
...
&lt;/<span style="font-weight: bold;">script</span>&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
